using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Mvc.Ajax;
using NexForum.WebApplication.Models;

namespace NexForum.WebApplication.Controllers {
	public class DiscussionsController : Controller {
		NexForumDataContext db = new NexForumDataContext();
		
		//
		// GET: /Discussions/

		public ActionResult Index() {

			ViewData.Model = db.Discussions.ToList();

			return View();
		}

		//
		// GET: /Discussions/Details/5

		public ActionResult Details(int id) {
			return View();
		}

		//
		// GET: /Discussions/Create

		public ActionResult Create() {
			return View();
		}

		//
		// POST: /Discussions/Create

		[AcceptVerbs(HttpVerbs.Post)]
		public ActionResult Create(Discussion discussion) {
			try {
				UpdateModel<Discussion>(discussion);
				db.SubmitChanges();
				return RedirectToAction("Index");
			} catch {
				return View();
			}
		}

		//
		// GET: /Discussions/Edit/5

		public ActionResult Edit(int id) {
			var discussion = db.Discussions.First(d => d.Id == id);
			ViewData.Model = discussion;
			return View();
		}

		//
		// POST: /Discussions/Edit/5

		[AcceptVerbs(HttpVerbs.Post)]
		public ActionResult Edit(int id, FormCollection collection) {
			var discussion = db.Discussions.First(d => d.Id == id);
			
			try {

				// Allowed Properties
				var whitelist = new[] { "ParentCategoryId", "Name", "CreateDate", "Active" };
				
				// Update the model			
				UpdateModel(discussion, whitelist);

				// Save Changes
				db.SubmitChanges();

				return RedirectToAction("Index");
			} catch {
				return View(discussion);
			}
		}
	}
}
